package com.pms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pms.entity.Users;
import com.pms.service.UserService;
import com.pms.util.GetSession;

	/**
	 * @author handy
	 * 2013-03-11
	 */
public class Login extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String name = request.getParameter("username");
		String pwd = request.getParameter("password");
		
		System.out.println("username:" + name + "\t password:" + pwd);
		Users user = new UserService().findByNameAndPwd(name, pwd);
		if(user != null){
			
			GetSession.getSession(request).setAttribute("user", user);
			//request.getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);
			response.sendRedirect("tomain.jspx");
		}else{
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}
			
	}
	
}
